"""
作者:张琪
页面:商品管理-添加新商品-其他信息
"""
import time
from Common.Base import Base, open_browser

otherinfo_url = 'http://172.16.11.206:8080/ecshop/admin/index.php'


class GoodsOtherInformation(Base):
    """制作定位器"""
    cancel_submit_loc = ('css selector', 'div.panel-cross span')  # Ｘ按钮
    ecseshop_loc = ('id', 'cloudLogin')  # ECShop定位
    username_loc = ('name', 'username')  # 用户名输入框
    password_loc = ('name', 'password')  # 密码输入框
    login_submit_loc = ('class name', 'btn-a')  # 登录按钮
    goods_management_frame = ('id', 'menu-frame')  # 商品管理链接frame
    goods_management = ('class name', 'icon-goods')  # 商品管理链接
    add_goods = ('css selector', 'a[href*="goods.php?act=add"]')  # 添加新商品链接
    general_information_frame = ('id', 'main-frame')  # 通用信息frame
    goods_name = ('name', 'goods_name')  # 商品名称输入框
    goods_categories_first = ('css selector', 'select[name="cat_id"]>option:nth-of-type(2)')  # 选择商品类别为家用电器
    mix_tab = ('xpath', '//*[@id="mix-tab"]')  # 其他信息页
    define = ('css selector', 'input[value*="确定"]')  # 确定按钮
    reset = ('css selector', 'input[value*="重置"]')  # 重置按钮
    goods_weight = ('name', 'goods_weight')  # 商品重量输入框
    weight_unit = ('css selector', 'select[name="weight_unit"]>option:nth-of-type(2)')  # 选择商品重量单位为克
    stock_num_message = ('css selector', 'a[href*="noticeStorage"]')  # 商品库存提示信息按钮
    stock_num = ('css selector', 'input[name*="goods_number"]')  # 商品库存输入框
    warn_number = ('css selector', 'input[name*="warn_number"]')  # 库存警告数量输入框
    join_recommendation = ('class name', 'label')  # 加入推荐多选框
    best_goods = ('name', 'is_best')  # 精品按钮
    new_goods = ('name', 'is_new')  # 新品按钮
    hot_goods = ('name', 'is_hot')  # 热销按钮
    on_sale = ('name', 'is_on_sale')  # 上架按钮
    alone_sale = ('name', 'is_alone_sale')  # 普通销售按钮
    shipping = ('name', 'is_shipping')  # 免运费商品
    goods_keywords = ('name', 'keywords')  # 商品关键词输入框
    goods_brief = ('name', 'goods_brief')  # 商品简单描述输入框
    comment = ('css selector', 'a[href*="noticeSellerNote"]')  # 商家备注提示信息
    comment_box = ('name', 'seller_note')  # 商家备注输入框
    add_goods_name = ('css selector', 'td.first-cell>span')  # 新增商品
    is_on_sale_submit = ('xpath', '/html/body/div[3]/form/input[1]')  # 上架按钮
    search_submit = ('xpath', '/html/body/div[3]/form/button')  # 搜索按钮

    """封装操作层"""

    def login(self, username, password):
        """登录账号"""
        self.click(self.cancel_submit_loc)
        time.sleep(1)
        self.click(self.ecseshop_loc)
        self.send_keys(self.username_loc, username)
        self.send_keys(self.password_loc, password)
        self.click(self.login_submit_loc)

    def get_into_goods(self):
        """进入商品"""
        self.into_frame(self.goods_management_frame)
        self.click(self.goods_management)
        self.out_frame()

    def get_into_add_goods(self):
        """进入添加商品"""
        self.into_frame(self.goods_management_frame)
        self.click(self.add_goods)
        self.out_frame()

    def get_into_other_page(self, name):
        """进入其他信息页操作"""
        self.into_frame(self.general_information_frame)
        self.send_keys(self.goods_name, name)
        self.click(self.goods_categories_first)
        time.sleep(1)
        self.click(self.mix_tab)


    def goods_weight_input(self, num):
        """商品重量输入框"""
        self.send_keys(self.goods_weight, num)

    def weight_unit_click(self):
        """选择商品重量单位为克"""
        self.click(self.weight_unit)

    def stock_num_message_click(self):
        """点击商品库存提示信息按钮"""
        time.sleep(2)
        self.click(self.weight_unit)

    def stock_num_input(self, num):
        """商品库存输入框"""
        self.clear(self.stock_num)
        self.send_keys(self.stock_num, num)

    def warn_number_input(self, num):
        """库存警告数量输入框"""
        self.clear(self.warn_number)
        self.send_keys(self.warn_number, num)

    def recommendation(self):
        """推荐多选按钮"""
        self.click(self.best_goods)
        self.click(self.new_goods)
        self.click(self.hot_goods)

    def on_sale_click(self):
        """上架按钮"""
        self.click(self.on_sale)

    def alone_sale_click(self):
        """普通销售按钮"""
        time.sleep(2)
        self.click(self.alone_sale)

    def shipping_click(self):
        """免运费商品按钮"""
        time.sleep(2)
        self.click(self.shipping)

    def goods_keywords_input(self, text):
        """商品关键词输入框"""
        self.send_keys(self.goods_keywords, text)

    def goods_brief_input(self, text):
        """商品简单描述输入框"""
        self.send_keys(self.goods_brief, text)

    def comment_click(self):
        """商家备注信息按钮"""
        self.click(self.comment)

    def comment_box_input(self, text):
        """商家备注输入框"""
        self.send_keys(self.comment_box, text)

    def stock_num_submit(self):
        """判断商品库存提示信息是否正常显示"""
        return self.message_submit(self.stock_num_message)

    def is_alone_sale_submit(self):
        """判断普通销售按钮是否被选中"""
        return self.is_selected_submit_radio(self.alone_sale)

    def is_shipping_submit(self):
        """判断免运费商品是否被选中"""
        return self.is_selected_submit_radio(self.shipping)

    def comment_submit(self):
        """判断商家备注提示信息是否正常显示"""
        return self.message_submit(self.comment)

    def define_click(self):
        """点击确定按钮"""
        self.click(self.define)
        time.sleep(8)
    def is_add_success(self,text):
        """
        判断新增商品是否成功,如果成功,返回TRUE,反之返回False
        @return:
        """
        self.click(self.is_on_sale_submit)
        self.click(self.search_submit)
        return self.is_text_equal_in_element(self.add_goods_name,text)


if __name__ == '__main__':
    login = GoodsOtherInformation(open_browser())  # 打开浏览器
    login.open_url(otherinfo_url)  # 进入商品详情页面
    login.login('root', 'root123456')  # 输入用户名,密码
    login.get_into_goods()  # 进入商品管理
    login.get_into_add_goods()  # 进入添加商品
    login.get_into_other_page('999789')  # 进入其他信息页操作
    login.goods_weight_input("456")  # 输入商品重量
    login.weight_unit_click()  # 选择商品重量
    login.stock_num_message_click()  # 点击商品库存提示信息按钮
    login.stock_num_input('999999')  # 输入商品库存
    login.warn_number_input('50')  # 输入库存警告数量
    login.recommendation()  # 点击推荐多选按钮
    login.on_sale_click()  # 点击上架按钮
    login.alone_sale_click()  # 点击普通销售按钮
    login.shipping_click()  # 点击免运费商品
    login.goods_keywords_input('免费')  # 输入商品关键词
    login.goods_brief_input('免费')  # 输入商品简单描述
    login.comment_click()  # 点击商家备注信息按钮
    login.comment_box_input('免费')  # 输入商家备注
